home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / Show2D.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-16  |  7.4 KB  |  235 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmShow2D 
  4.    Caption         =   "Show2D"
  5.    ClientHeight    =   4365
  6.    ClientLeft      =   2415
  7.    ClientTop       =   1650
  8.    ClientWidth     =   5355
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   4365
  12.    ScaleWidth      =   5355
  13.    Begin MSComDlg.CommonDialog dlgFile 
  14.       Left            =   240
  15.       Top             =   360
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.       CancelError     =   -1  'True
  20.    End
  21.    Begin VB.PictureBox picCanvas 
  22.       Height          =   2415
  23.       Left            =   0
  24.       ScaleHeight     =   157
  25.       ScaleMode       =   3  'Pixel
  26.       ScaleWidth      =   157
  27.       TabIndex        =   0
  28.       Top             =   0
  29.       Width           =   2415
  30.    End
  31.    Begin VB.Menu mnuFile 
  32.       Caption         =   "&File"
  33.       Begin VB.Menu mnuFileOpen 
  34.          Caption         =   "&Open..."
  35.          Shortcut        =   ^O
  36.       End
  37.       Begin VB.Menu mnuFileSave2D 
  38.          Caption         =   "Save &2D File..."
  39.          Shortcut        =   ^S
  40.       End
  41.       Begin VB.Menu mnuFileSaveMetafile 
  42.          Caption         =   "Save &Metafile..."
  43.          Shortcut        =   ^M
  44.       End
  45.    End
  46. Attribute VB_Name = "frmShow2D"
  47. Attribute VB_GlobalNameSpace = False
  48. Attribute VB_Creatable = False
  49. Attribute VB_PredeclaredId = True
  50. Attribute VB_Exposed = False
  51. Option Explicit
  52. ' The scene that contains all other objects.
  53. Private TheScene As TwoDObject
  54. Private Declare Function CreateMetaFile Lib "gdi32" Alias "CreateMetaFileA" (ByVal lpString As String) As Long
  55. Private Declare Function CloseMetaFile Lib "gdi32" (ByVal hmf As Long) As Long
  56. Private Declare Function DeleteMetaFile Lib "gdi32" (ByVal hmf As Long) As Long
  57. Private Declare Function SetWindowExtEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE) As Long
  58. Private Type SIZE
  59.     cx As Long
  60.     cy As Long
  61. End Type
  62. ' Save the object serialization.
  63. Private Sub mnuFileSave2D_Click()
  64. Dim file_name As String
  65. Dim fnum As Integer
  66.     If TheScene Is Nothing Then
  67.         MsgBox "No scene is loaded."
  68.         Exit Sub
  69.     End If
  70.     ' Allow the user to pick a file.
  71.     On Error Resume Next
  72.     dlgFile.Filter = _
  73.         "2D Files (*.2d)|*.2d|" & _
  74.         "All Files (*.*)|*.*"
  75.     dlgFile.Flags = _
  76.         cdlOFNOverwritePrompt Or _
  77.         cdlOFNPathMustExist Or _
  78.         cdlOFNHideReadOnly
  79.     dlgFile.ShowSave
  80.     If Err.Number = cdlCancel Then
  81.         ' The user canceled.
  82.         Unload dlgFile
  83.         Exit Sub
  84.     ElseIf Err.Number <> 0 Then
  85.         ' Unknown error.
  86.         Unload dlgFile
  87.         MsgBox "Error " & Format$(Err.Number) & _
  88.             " selecting file." & vbCrLf & _
  89.             Err.Description, vbExclamation
  90.         Exit Sub
  91.     End If
  92.     On Error GoTo Save2DFileError
  93.     ' Get the file name.
  94.     file_name = dlgFile.FileName
  95.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  96.         - Len(dlgFile.FileTitle) - 1)
  97.     Caption = "Show2D [" & dlgFile.FileTitle & "]"
  98.     ' Open the file.
  99.     fnum = FreeFile
  100.     Open file_name For Output As fnum
  101.     ' Write the serialization into the file.
  102.     Print #fnum, TheScene.Serialization
  103.     ' Close the file.
  104.     Close fnum
  105.     Exit Sub
  106. Save2DFileError:
  107.     MsgBox "Error " & Format$(Err.Number) & _
  108.         " saving file." & vbCrLf & _
  109.         Err.Description, vbExclamation
  110.     Exit Sub
  111. End Sub
  112. Private Sub mnuFileSaveMetafile_Click()
  113. Dim file_name As String
  114. Dim mf_dc As Long
  115. Dim hmf As Long
  116. Dim old_size As SIZE
  117.     If TheScene Is Nothing Then
  118.         MsgBox "No scene is loaded."
  119.         Exit Sub
  120.     End If
  121.     ' Allow the user to pick a file.
  122.     On Error Resume Next
  123.     dlgFile.Filter = _
  124.         "Metafiles (*.wmf)|*.wmf|" & _
  125.         "All Files (*.*)|*.*"
  126.     dlgFile.Flags = _
  127.         cdlOFNOverwritePrompt Or _
  128.         cdlOFNPathMustExist Or _
  129.         cdlOFNHideReadOnly
  130.     dlgFile.ShowSave
  131.     If Err.Number = cdlCancel Then
  132.         ' The user canceled.
  133.         Unload dlgFile
  134.         Exit Sub
  135.     ElseIf Err.Number <> 0 Then
  136.         ' Unknown error.
  137.         Unload dlgFile
  138.         MsgBox "Error " & Format$(Err.Number) & _
  139.             " selecting file." & vbCrLf & _
  140.             Err.Description, vbExclamation
  141.         Exit Sub
  142.     End If
  143.     On Error GoTo SaveMetafileError
  144.     ' Get the file name.
  145.     file_name = dlgFile.FileName
  146.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  147.         - Len(dlgFile.FileTitle) - 1)
  148.     Caption = "Show2D [" & dlgFile.FileTitle & "]"
  149.     ' Create the metafile.
  150.     mf_dc = CreateMetaFile(ByVal file_name)
  151.     If mf_dc = 0 Then
  152.         MsgBox "Error creating the metafile.", vbExclamation
  153.         Exit Sub
  154.     End If
  155.     ' Set the metafile's size to something reasonable.
  156.     SetWindowExtEx mf_dc, picCanvas.ScaleWidth, _
  157.         picCanvas.ScaleHeight, old_size
  158.     ' Draw in the metafile.
  159.     TheScene.DrawInMetafile mf_dc
  160.     ' Close the metafile.
  161.     hmf = CloseMetaFile(mf_dc)
  162.     If hmf = 0 Then
  163.         MsgBox "Error closing the metafile.", vbExclamation
  164.     End If
  165.     ' Delete the metafile to free resources.
  166.     If DeleteMetaFile(hmf) = 0 Then
  167.         MsgBox "Error deleting the metafile.", vbExclamation
  168.     End If
  169.     Exit Sub
  170. SaveMetafileError:
  171.     MsgBox "Error " & Format$(Err.Number) & _
  172.         " saving file." & vbCrLf & _
  173.         Err.Description, vbExclamation
  174.     Exit Sub
  175. End Sub
  176. Private Sub picCanvas_Paint()
  177.     If Not TheScene Is Nothing Then TheScene.Draw picCanvas
  178. End Sub
  179. Private Sub Form_Load()
  180.     dlgFile.InitDir = App.Path
  181.     dlgFile.Filter = "TwoD Files (*.2d)|*.2d|" & _
  182.         "All Files (*.*)|*.*"
  183.     dlgFile.CancelError = True
  184. End Sub
  185. Private Sub Form_Resize()
  186.     picCanvas.Move 0, 0, ScaleWidth, ScaleHeight
  187. End Sub
  188. Private Sub mnuFileOpen_Click()
  189. Dim file_name As String
  190. Dim fnum As Integer
  191. Dim the_serialization As String
  192. Dim token_name As String
  193. Dim token_value As String
  194.     ' Allow the user to pick a file.
  195.     On Error Resume Next
  196.     dlgFile.Flags = cdlOFNExplorer Or _
  197.         cdlOFNFileMustExist Or _
  198.         cdlOFNHideReadOnly Or _
  199.         cdlOFNLongNames
  200.     dlgFile.ShowOpen
  201.     If Err.Number = cdlCancel Then
  202.         Unload dlgFile
  203.         Exit Sub
  204.     ElseIf Err.Number <> 0 Then
  205.         Unload dlgFile
  206.         Beep
  207.         MsgBox "Error selecting file.", , vbExclamation
  208.         Exit Sub
  209.     End If
  210.     On Error GoTo 0
  211.     ' Read the picture's serialization.
  212.     file_name = dlgFile.FileName
  213.     fnum = FreeFile
  214.     Open file_name For Input As #fnum
  215.     the_serialization = RemoveNonPrintables(Input$(LOF(fnum), fnum))
  216.     Close fnum
  217.     ' Make sure this is a TwoDScene serialization.
  218.     GetNamedToken the_serialization, token_name, token_value
  219.     If token_name <> "TwoDScene" Then
  220.         ' This is not a valid serialization.
  221.         MsgBox "This is not a valid TwoDScene serialization."
  222.     Else
  223.         Caption = "Show2D [" & dlgFile.FileTitle & "]"
  224.         dlgFile.InitDir = Left$(file_name, Len(file_name) _
  225.             - Len(dlgFile.FileTitle) - 1)
  226.         ' Initialize the new scene.
  227.         Set TheScene = New TwoDScene
  228.         TheScene.Serialization = token_value
  229.     End If
  230.     ' Display the scene.
  231.     picCanvas.Cls
  232.     TheScene.Draw picCanvas
  233.     picCanvas.Refresh
  234. End Sub
  235.